
#include <iostream>
#include <fstream>
#include <String>
using namespace std;

void menu()
{
	cout<<"	1---新建学生健康表--------------------------------"<<endl;
	cout<<"	2---向学生健康表插入学生信息----------------------"<<endl;
	cout<<"	3---在健康表删除学生信息--------------------------"<<endl;
	cout<<"	4---从文件中读取健康表信息------------------------"<<endl;
	cout<<"	5---向文件写入学生健康表信息----------------------"<<endl;
	cout<<"	6---在健康表中查询学生信息（按学生学号来进行查找）"<<endl;
	cout<<"	7---在屏幕中输出全部学生信息----------------------"<<endl;
	cout<<"	8-- 退出------------------------------------------"<<endl;
}


struct StudentInformation{
	char id[12];
	char name[12];
	char birthday[12];
	char sex[12];
	char bodyHealth[12];

	void input(){
	cout<<"请输入学生信息："<<endl;	
	cout<<"请输入学生的学号:";
	cin>>id;
	cout<<"请输入学生的名字:";
	cin>>name;
	cout<<"请输入学生的性别:";
	cin>>sex;
	cout<<"请输入学生生日的日期(年月日格式为:20160901):";
	cin>>birthday;
	cout<<"请输入学生的健康情况（良好或差）:";
	cin>>bodyHealth;
	cout<<endl;
}
	void output(){
		cout<<"该学生学生信息为:"<<endl;
		cout<<"学生的学号为:"<<id<<endl;
		cout<<"学生的姓名为:"<<name<<endl;
		cout<<"学生的性别为:"<<sex<<endl;
		cout<<"学生的出生日期为:"<<birthday<<endl;
		cout<<"学生的健康状况为:"<<bodyHealth<<endl;
		cout<<endl;
	}
};
ostream & operator << ( ostream & out, const StudentInformation & x )
{
    out << "学号:"<< x.id << endl;
    out << "姓名:"<< x.name  << endl;
    out << "性别:"<< x.sex << endl;
    out << "出生日期:"<< x.birthday << endl;
    out << "健康状况:"<<x.bodyHealth << endl;
    return out;
}

class Student{
public:
	Student ();
	Student (int sz);
	~Student(){}
	void MakeEmpty();
	void SetData();
	void Search(char ID[12]);
	bool Insert(int i,StudentInformation &s1);
	void print ();
	void Remove(int i,StudentInformation &s2);
	void Read();
	void Write();
private:
	StudentInformation *stu;
	int last;
	int maxSize;
};
int main (){
	Student s1,temp;
	StudentInformation s2;
	int i,last;
	last = 1;
	char ID[12];
	while (last != 8){
		menu();
		int choose;
		cout<<" 请输入你的选择（1~8）:";
		cin>>choose;
		switch (choose)
		{
		case 1:
			s1.SetData();
			break;
		case 2:
			cout<<"在哪一个位置后插入学生信息 i=?";
			cin>>i;
			s1.Insert(i,s2);
			break;
		case 3:
			cout<<"删除第i个元素 i=? ";
			cin>>i;
			s1.Remove(i,s2);
			break;
		case 4:
			s1.Read();			
			break;
		case 5:
			s1.Write();
			break;
		case 6:
			cout<<"请输入查询学生的学号: ";
			cin>>ID;
			s1.Search(ID);
			break;
		case 7:
			s1.print();
			break;
		case 8:
			last = 8;
			exit(1);
			break;
		default:
			cout<<"choose输入错误,请重新输入choose值"<<endl<<"choose:"<<endl;
	}
}
	return 0;
}

//methods

void Student::Write()
{
	ofstream fop;								 //以二进制方式打开stu.txt文件
	fop.open("stu.txt",ios::out|ios::binary|ios::trunc);
	if(fop.fail())                     //文件打开失败
	{
		cout<<"文件打开失败！"<<endl;
	}
	for(int i=0;i<=last; i++)         //把各字符信息写入文件
	{
		// fop.write((char*)&stu[i],sizeof(stu[i]));
		// flush(cout);
		fop << stu[i];
	}
	cout<<"文件写入成功"<<endl;
}
void Student::Read()
{
	ifstream fip;        //以二进制方式打开stu.txt文件
	fip.open("stu.txt",ios::binary|ios::in);
	if(fip.fail())       //文件打开失败
	{
		cout<<"文件打开失败！"<<endl;
	}
	char s;
	while(fip.get(s))
	{
		cout<<s;
		// fip.read((char*)&s,sizeof(s));   //从文件中读入一个学生的信息
		// if(!fip)
		// {
		// 	break;	
		// }
		// student.ReadInsert(s);
	}
	fip.close();
	cout<<"读写成功"<<endl;
}


void Student::Remove(int i,StudentInformation &s2){
	if (last == -1){
		cout<<"顺序表为空,无法删除"<<endl;
	}
	if (i < 1||i > last+1){
		cout<<"参数i不合理"<<endl;
	}
	s2=stu[i-1];
	cout<<"您所成功删除的结点为"<<endl;
	s2.output();
	for (int j = i;j <= last ;j++){
			stu[j-1] = stu[j];
		}
		last--;
}

void Student::print(){
	cout<<"顺序表中存储信息如下:"<<endl;
	for (int i = 0;i <= last;i++){
		stu[i].output();
	}
}

Student::Student (){
	maxSize = 100;
	last = -1;
	stu = new StudentInformation[maxSize];
	if (stu == NULL){
		cout<<"存储分配错误"<<endl;
		exit(1);
	}
}

Student::Student (int sz){
	if(sz>0){
		maxSize = sz;
		last = -1;
		stu = new StudentInformation[maxSize];
		if (stu == NULL){
			cout<<"存储分配错误"<<endl;
			exit(1);
		}
		else 	{
		cout<<"存储分配错误"<<endl;
		}
	}
}

void Student::MakeEmpty(){
	last = -1;
}

void Student::SetData(){
	int count;
	cout<<"输入学生的个数count = ";
	cin>>count;
	for (int i = 0;i < count;i++)
	{
		stu[i].input();
		last++;
	}

}
void Student::Search(char ID[12]){
	for (int i = 0;i<=last;i++){
		if (ID == stu[i].id){
			stu[i].output();
		}
	}
	cout<<"未找到此学生"<<endl;
}

bool Student::Insert(int i,StudentInformation &s1){
	if (last == maxSize - 1){
		cout<<"表满，不能插入"<<endl;
	return false;
	}
	if (i<0||i>last+1){
		cout<<"参数i不合理"<<endl;
		return false;
	}
	s1.input();
	for (int j = last;j>=i;j--){
		stu[j+1] = stu[j];
	}
	stu[i] = s1;
	last++;
	cout<<"插入成功"<<endl;
	return true;
}
